var gulp = require('gulp');
const babel = require('gulp-babel');
const ts = require('gulp-typescript');
const pug = require('gulp-pug');
const rename = require('gulp-rename');
const plumber = require('gulp-plumber');
const data = require('gulp-data');
const babelConfig = require('./babel.config');
const { args, fileExists } = require('./functions.js');
const { readMock } = require('./gulp-inc/readmock')
const { test } = require('./gulp-inc/test')

const tsProject = ts.createProject('tsconfig.json', { isolatedModules: false });
 
function compileJs(){
  return gulp.src('src/**/*.ts')
  .pipe(babel(babelConfig))
  .pipe(gulp.dest('dist'))
}

// 校验并生成类型声明文件
function generateDts(){
  var tsResult = gulp.src('src/**/*.ts')
      .pipe(tsProject());
  return tsResult.dts.pipe(gulp.dest('dist'))
}

function compileComponent() {
  const com = args()
  
  return gulp.src([`src/components/${com.com}/**/*.ts`,`src/components/${com.com}/**/*.js`])
  .pipe(babel(babelConfig))
  .pipe(gulp.dest(`dist/components/${com.com}`))
}

function compilePugs() {
  const com = args()
  return gulp.src([`src/components/${com.com}/**/*.pug`])
  .pipe(data(function(){
    return {text: "xxxxx"}
  }))
  .pipe( test() )
  .pipe(pug())
  .pipe(rename({
    name: 'enter'
  }))
  .pipe(gulp.dest(`dist/components/${com.com}`))
}

const buildComponent = gulp.parallel(compileComponent);
const build = gulp.parallel(compileJs, generateDts);

module.exports.build = build
module.exports.buildComponent = buildComponent
module.exports.compilePugs = compilePugs
